//
//  ZHViewController.m
//  LBProjectCategoryViewController
//
//  Created by luckyBoyRoy@163.com on 11/14/2017.
//  Copyright (c) 2017 luckyBoyRoy@163.com. All rights reserved.
//

#import "ZHViewController.h"
#import <LBCategoryApplication.h>
#import "ZHCateogryPartViewController.h"
#import "ZHBookingViewController.h"
#import "ZHPullViewController.h"

@interface ZHViewController ()

@property (nonatomic, strong) LBCategoryModel *selectedModel;

@property (nonatomic, strong) UIButton *showButton;

@property (nonatomic, strong) NSMutableArray *selelectedCodeArray;

@end

@implementation ZHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.selelectedCodeArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *oneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [oneButton setTitle:@"通用分类" forState:0];
    [oneButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [oneButton addTarget:self action:@selector(categoryJoin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:oneButton];
    oneButton.tag = 1;
    oneButton.frame = CGRectMake(100, 100, 200, 50);
    
    
    UIButton *twoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [twoButton setTitle:@"订阅分类" forState:0];
    [twoButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [twoButton addTarget:self action:@selector(categoryJoin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:twoButton];
    twoButton.tag = 2;
    twoButton.frame = CGRectMake(100, 100+50, 200, 50);
    
    UIButton *threeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [threeButton setTitle:@"弹窗分类" forState:0];
    [threeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [threeButton addTarget:self action:@selector(categoryJoin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:threeButton];
    threeButton.tag = 3;
    threeButton.frame = CGRectMake(100, 100+50 * 2, 200, 50);
    
    UIButton *showButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [showButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    showButton.backgroundColor = [UIColor blue];
    [self.view addSubview:showButton];
    showButton.tag = 3;
    showButton.frame = CGRectMake(10, 100+50 * 3,[[UIScreen mainScreen] bounds].size.width-20,200);
    self.showButton = showButton;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"分类模块测试" style:0 target:self action:@selector(rightBarAction)];
}

- (void)showtitleBy:(NSArray *)selectedArray parm:(id)parm
{
    NSMutableString *str = [NSMutableString new];
    [self.selelectedCodeArray removeAllObjects];
    [selectedArray enumerateObjectsUsingBlock:^(LBCategoryModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.name.length) {
            [str appendString:obj.name];
        }
        if (obj.code) {
            [self.selelectedCodeArray addObject:obj.code];
        }
    }];
    
    [self.showButton setTitle:str forState:UIControlStateNormal];
    
    if ([parm integerValue]) {
        [self.showButton setTitle:@"全部分类" forState:UIControlStateNormal];
    }
}

- (void)categoryJoin:(UIButton *)sender {
    NSInteger index = sender.tag;
    __weak __typeof(&*self)weakSelf = self;
    if (1 == index) {
        ZHCateogryPartViewController *parViewController = [[ZHCateogryPartViewController alloc]init];
        [parViewController setSelelectedCodeArray:self.selelectedCodeArray];
        [parViewController setViewBlock:^(NSArray<LBCategoryModel *> *modelArray, id parm) {
            [weakSelf showtitleBy:modelArray parm:parm];
        }];
        [self.navigationController pushViewController:parViewController animated:YES];
    } else if (2 == index) {
        ZHBookingViewController *parViewController = [[ZHBookingViewController alloc]init];
        [parViewController setSelelectedCodeArray:self.selelectedCodeArray];
        [parViewController setViewBlock:^(NSArray<LBCategoryModel *> *modelArray, id parm) {
        [weakSelf showtitleBy:modelArray parm:parm];
    }];
        [self.navigationController pushViewController:parViewController animated:YES];
    } else if (3 == index) {
        ZHPullViewController *parViewController = [[ZHPullViewController alloc]init];
        [parViewController setViewBlock:^(NSArray<LBCategoryModel *> *modelArray, id parm) {
        [weakSelf showtitleBy:modelArray parm:parm];
    }];
        [self.navigationController pushViewController:parViewController animated:YES];
    }
    
//    if (1 == sender.tag) {
//        /*
//         category/selected/:codeType/:codeStrcing/:isSingleSelected/:maxSelectedLevel/:blockKey
//         */
//        NSString *urlStr = [NSString stringWithFormat:@"category/selected/9/%@/1/4",@"5001003002008"];
//        [LBCategoryApplication openURL:urlStr callBack:^(NSArray<LBCategoryModel *> *resultArray) {
//
//        }];
//    } else {
//        /*
//         @"category/partSelected/:codeType/:codeStrcing/:isSingleSelected/:maxSelectedLevel/:blockKey"
//         */
//        NSString *urlStr;
//        if (self.selectedModel) {
//            urlStr = [NSString stringWithFormat:@"category/partSelected/11/%@/1/1/4",self.selectedModel.code];
//        } else {
//            urlStr = @"category/partSelected/11/0/1/1/4";
//        }
//        [LBCategoryApplication openURL:urlStr callBack:^(NSArray<LBCategoryModel *> *resultArray) {
//            self.selectedModel = [resultArray firstObject];
//        }];
//    }
}

- (void)rightBarAction
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
